//
//  HomeNavigationBar.swift
//  WeiboDemo
//
//  Created by 姬向阳 on 2020/4/19.
//  Copyright © 2020 SUNNYJ. All rights reserved.
//

import SwiftUI

private var kLabelWidth : CGFloat = 60
private var kLabelHeight : CGFloat = 24

struct HomeNavigationBar: View {
    @Binding var leftPercent : CGFloat // 0 for left , 1 for right
    
    var body: some View {
        HStack (alignment:.top, spacing: 0){
            // 相机Button
            Button(action: {
                print("Clicked camera button.")
            }) {
                Image(systemName: "camera")
                    .resizable()
                    .scaledToFit()
                    .frame(width: kLabelHeight, height: kLabelHeight)
            }
            .padding(.horizontal,15)
            .padding(.top, 5)
            .foregroundColor(.black)
            Spacer()
            VStack (spacing: 3){
                HStack {
                    Text("推荐")
                        .bold()
                        .frame(width: kLabelWidth ,height: kLabelHeight)
                        .opacity(Double(1 - leftPercent * 0.5))
                        .onTapGesture {
                            withAnimation {
                                self.leftPercent = 0
                            }
                    }
                    Spacer()
                    Text("热门")
                        .bold()
                        .frame(width: kLabelWidth ,height: kLabelHeight)
                        .opacity(Double(0.5 + leftPercent * 0.5))
                        .onTapGesture {
                            withAnimation {
                                self.leftPercent = 1
                            }
                    }
                }
                .padding(.top, 5)
                .font(.system(size: 20))
                // 最大化自己 geometry 可以读取自身的size
                GeometryReader{ geometry in
                    Rectangle()
                        .cornerRadius(2)
                        .frame(width: 30,height: 4)
                        .foregroundColor(.orange)
                        .offset(x: geometry.size.width * (self.leftPercent - 0.5) + kLabelWidth * (0.5 - self.leftPercent))
                }
                .frame(height: 6)
            }
            .frame(width: UIScreen.main.bounds.width / 2)
            
            Spacer()
            // 加号Button
            Button(action: {
                print("Clicked add button.")
            }) {
                Image(systemName: "plus.circle.fill")
                    .resizable()
                    .scaledToFit()
                    .frame(width: kLabelHeight, height: kLabelHeight)
            }
            .padding(.horizontal,15)
            .padding(.top, 5)
            .foregroundColor(.orange)
        }
        .frame(width: UIScreen.main.bounds.width)
    }
}

struct HomeNavigationBar_Previews: PreviewProvider {
    static var previews: some View {
        HomeNavigationBar(leftPercent: .constant(0))
    }
}
